<?php

namespace App\Http\Controllers\Api;

use App\Models\Topic;
use App\Services\PublishExamineService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ExamineCenterController extends Controller
{
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (!auth('api')->check()) {
                return $this->error('请先登录', [], 40001, 401);
            }

            $member = auth('api')->user();

            if (!$member->publisher || !$member->publisher->is_examine) return $this->error('无权限操作');

            return $next($request);
        });
    }

    public function topic(Request $request, PublishExamineService $service)
    {
        $list = $service->topic(auth('api')->user()->id, $this->getMiniType(), $request->input('status'));

        return $this->_success(compact('list'));
    }

    public function topicExamine(Request $request, PublishExamineService $service)
    {
        $validatedData = Validator::make(
            $request->all(),
            [
                'id'     => 'required|integer',
                'status' => 'required|in:1,2',
            ],
            [
                'id.required'     => 'id不能为空',
                'id.integer'      => 'id参数错误',
                'status.required' => '状态不能为空',
                'status.in'       => '状态参数错误',
            ])->validate();

        $service->topicExamine($validatedData['id'], $validatedData['status'], $this->getMiniType());

        return $this->success('审核成功');
    }

    public function topicSetHot(int $id, PublishExamineService $service)
    {
        $service->topicSetHot($id, $this->getMiniType());

        return $this->success('操作成功');
    }

    public function topicDelete(int $id, PublishExamineService $service)
    {
        $service->topicDelete($id, $this->getMiniType());

        return $this->success('操作成功');
    }

    public function comment(Request $request, PublishExamineService $service)
    {
        $validatedData = Validator::make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3',
                'page' => 'required|integer',
            ],
            [
                'type.required' => '类型不能为空',
                'type.in'       => '类型参数错误',
                'page.required' => 'page不能为空',
                'page.integer'  => 'page参数错误',
            ])->validate();

        return $this->_success(['list' => $service->comment($validatedData['type'], $this->getMiniType())]);
    }

    public function commentExamine(Request $request, PublishExamineService $service)
    {
        $validatedData = Validator::make(
            $request->all(),
            [
                'id'     => 'required|integer',
                'status' => 'required|in:1,2',
                'type'   => 'required|in:1,2,3',
            ],
            [
                'id.required'     => 'id不能为空',
                'id.integer'      => 'id参数错误',
                'status.required' => '状态不能为空',
                'status.in'       => '状态参数错误',
                'type.required'   => '类型不能为空',
                'type.in'         => '类型参数错误',
            ])->validate();

        $service->commentExamine($validatedData['id'], $validatedData['type'], $validatedData['status'], $this->getMiniType());

        return $this->success('操作成功');
    }

    public function commentDelete(Request $request, PublishExamineService $service)
    {
        $validatedData = Validator::make(
            $request->all(),
            [
                'id'   => 'required|integer',
                'type' => 'required|in:1,2,3',
            ],
            [
                'id.required'   => 'id不能为空',
                'id.integer'    => 'id参数错误',
                'type.required' => '类型不能为空',
                'type.in'       => '类型参数错误',
            ])->validate();

        $service->commentDelete($validatedData['id'], $validatedData['type'], $this->getMiniType());

        return $this->success('操作成功');
    }

    public function msgBoard(Request $request, PublishExamineService $service)
    {
        $status = $request->input('status');
        return $this->_success(['list' => $service->msgBoard($status, $this->getMiniType())]);
    }

    public function msgBoardReply(Request $request, PublishExamineService $service)
    {
        $validatedData = Validator::make(
            $request->all(),
            [
                'id'      => 'required|integer',
                'content' => 'required|max:255',
            ],
            [
                'id.required'      => 'id不能为空',
                'id.integer'       => 'id参数错误',
                'content.required' => '回复内容不能为空',
                'content.max'      => '回复内容不能超过255个字符',
            ])->validate();

        $service->msgBoardReply(
            $validatedData['id'],
            $validatedData['content'],
            auth('api')->user()->id,
            $this->getMiniType()
        );

        return $this->success('操作成功');
    }
}
